package com.example.readphonefile

import android.Manifest
import android.content.ContentUris
import android.content.pm.PackageManager
import android.graphics.BitmapFactory
import android.net.Uri
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.provider.MediaStore
import android.util.Log
import android.view.View
import android.view.ViewGroup
import android.widget.Button
import android.widget.ImageView
import androidx.core.app.ActivityCompat
import androidx.core.content.ContextCompat
import androidx.viewpager.widget.PagerAdapter
import androidx.viewpager.widget.ViewPager
import java.util.*
import kotlin.collections.ArrayList

/**
 * 读取手机中的所有文件
 */
private const val TAG = "MainActivity"
class MainActivity : AppCompatActivity(), View.OnClickListener {
	private lateinit var btn_read_file: Button
	private val list=ArrayList<Uri>()
	private lateinit var viewPager:ViewPager
	override fun onCreate(savedInstanceState: Bundle?) {
		super.onCreate(savedInstanceState)
		setContentView(R.layout.activity_main)
		btn_read_file = findViewById(R.id.read_phone_file)
		this.viewPager=findViewById(R.id.viewpager_main)
		if(ContextCompat.checkSelfPermission(this,Manifest.permission.READ_EXTERNAL_STORAGE)!=
			PackageManager.PERMISSION_GRANTED){
			requestMyPermissions()
		}
		btn_read_file.setOnClickListener(this)


	}

	private fun requestMyPermissions() {
//		TODO("Not yet implemented")
		ActivityCompat.requestPermissions(this, arrayOf(Manifest.permission.READ_EXTERNAL_STORAGE),1)
	}

	override fun onRequestPermissionsResult(
		requestCode: Int,
		permissions: Array<out String>,
		grantResults: IntArray
	) {
		when(requestCode){
			1->{
				if(grantResults.isNotEmpty()&&grantResults[0]==PackageManager.PERMISSION_GRANTED){

				}
			}
		}
	}

	override fun onClick(v: View?) {
//		TODO("Not yet implemented")
		when (v?.id) {
			R.id.read_phone_file -> {
				contentResolver.query(
					MediaStore.Images.Media.EXTERNAL_CONTENT_URI, null,
					null, null, null
				)?.apply{
					while(moveToNext()){

						val id = getLong(getColumnIndexOrThrow(MediaStore.MediaColumns._ID))
						val uri = ContentUris.withAppendedId(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, id)
						list.add(uri)
					}
				}

				runOnUiThread{
					viewPager.adapter=MyPageAdapter()
				}
			}
		}
	}

	inner class MyPageAdapter:PagerAdapter(){
		override fun getCount(): Int {
//			TODO("Not yet implemented")
			return list.size
		}

		override fun isViewFromObject(view: View, `object`: Any): Boolean {
//			TODO("Not yet implemented")
			return view==`object`
		}

		override fun instantiateItem(container: ViewGroup, position: Int): Any {
//			return super.instantiateItem(container, position)
			val imageView=ImageView(this@MainActivity)
			val fd = contentResolver.openFileDescriptor(list.get(position), "r")
			if (fd != null) {
				val bitmap = BitmapFactory.decodeFileDescriptor(fd.fileDescriptor)
				fd.close()
//				assets
				imageView.setImageBitmap(bitmap)
				container.addView(imageView)
			}

			return imageView
		}

		override fun destroyItem(container: ViewGroup, position: Int, `object`: Any) {
			container.removeView(`object` as View)
		}
	}
}